

##############################################################################
## VARIABLES
## ------------

SRC_SRC = $(wildcard src/front/*.c) $(wildcard src/back/*.c) tests/common.c
SRC_OBJ = $(SRC_SRC:.c=.o)

CFLAGS = -Iinclude -Wall -Wstrict-prototypes -Wpointer-arith -Wundef -g -O2
LFLAGS=-lcrypto  -lpam

MAJVER=1
MINVER=0
REVVER=0

ROOTDIR = 
USRDIR  = $(ROOTDIR)/usr
NSSDIR  = $(ROOTDIR)/lib64
PAMDIR  = $(ROOTDIR)/lib64/security
ETCDIR  = $(ROOTDIR)/etc
LIBDIR  = $(USRDIR)/lib64
BINDIR  = $(USRDIR)/bin
SBINDIR = $(USRDIR)/sbin
INCDIR  = $(USRDIR)/include
DOCDIR  = $(USRDIR)/share/doc/saas-$(MAJVER).$(MINVER).$(REVVER)



##############################################################################
## TARGET ALIASES
## -----------------

all: libraries tests

libraries:                  \
	lib/libnss_saas.so.2    \
	lib/pam_saas.so         \
	lib/libsaas.so
#	lib/ksaas.so

tests: \
	tests/saas-test-authenticate                            \
	tests/saas-test-uid-to-username                         \
	tests/saas-test-username-to-uid                         \
	tests/saas-test-gid-to-groupname                        \
	tests/saas-test-groupname-to-gid                        \
	tests/saas-test-user-info                               \
	tests/saas-test-group-info



##############################################################################
## STANDARD COMPILATION
## ----------------------

%.o: %.c
	@echo "    [COMPILE] $^"
	@gcc -fPIC -Wall -ggdb -c $(CFLAGS) -o $@ $^



##############################################################################
## LIBRARIES
## ------------

## PAM
lib/pam_saas.so: $(SRC_OBJ) lib/pam_saas.o
	@echo "    [LINKING] $@"
	@gcc -shared -o $@ $^ -lpam $(LFLAGS)

## KRB5
lib/ksaas.so: $(SRC_OBJ) lib/ksaas.o
	@echo "    [LINKING] $@"
	@gcc -shared -o $@ $^ $(LFLAGS)

## C Library
lib/libsaas.so: $(SRC_OBJ) lib/libsaas.o
	@echo "    [LINKING] $@"
	@gcc -shared -o $@ $^ $(LFLAGS)

## NSS Library
lib/libnss_saas.so.2: $(SRC_OBJ) lib/libnss_saas.o
	@echo "    [LINKING] $@"
	@gcc -shared  $^  $(LFLAGS) -lpthread -lz -Wl,-soname -Wl,$@ -o $@



##############################################################################
## TEST EXECUTABLES
## ------------------

tests/saas-test-authenticate: $(SRC_OBJ) tests/saas-test-authenticate.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)


tests/saas-test-gid-to-groupname: $(SRC_OBJ) tests/saas-test-gid-to-groupname.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)

tests/saas-test-groupname-to-gid: $(SRC_OBJ) tests/saas-test-groupname-to-gid.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)

tests/saas-test-group-info: $(SRC_OBJ) tests/saas-test-group-info.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)


tests/saas-test-uid-to-username: $(SRC_OBJ) tests/saas-test-uid-to-username.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)

tests/saas-test-username-to-uid: $(SRC_OBJ) tests/saas-test-username-to-uid.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)

tests/saas-test-user-info: $(SRC_OBJ) tests/saas-test-user-info.o
	@echo "    [LINKING] $@"
	@gcc -ggdb -Wall -o $@ $^ $(LFLAGS)





##############################################################################
## CLEANUP
## ----------

clean:
	@find . -type f -name \*.o -exec rm -f {} \;
	@rm -f lib/*.so lib/*.so.2
	@rm -f tests/saas-test-authenticate
	@rm -f tests/saas-test-gid-to-groupname
	@rm -f tests/saas-test-group-info
	@rm -f tests/saas-test-groupname-to-gid
	@rm -f tests/saas-test-uid-to-username
	@rm -f tests/saas-test-user-info
	@rm -f tests/saas-test-username-to-uid



install:
	@if [ ! -d "$(LIBDIR)" ]; then mkdir -p "$(LIBDIR)"; chmod 755 "$(LIBDIR)"; fi
	install -D -m 0644 etc/saas.conf                         $(ETCDIR)/saas.conf
	install -D -m 0755 lib/pam_saas.so                       $(PAMDIR)/pam_saas.so
	install -D -m 0755 lib/libnss_saas.so.2                  $(NSSDIR)/libnss_saas.so.2
	rm -f $(LIBDIR)/libnss_saas.so
	ln -s $(NSSDIR)/libnss_saas.so.2                         $(LIBDIR)/libnss_saas.so
	symlinks -c $(LIBDIR)
	install -D -m 0755 lib/libsaas.so                        $(LIBDIR)/libsaas.so
	install -D -m 0644 include/saas.h                        $(INCDIR)/saas.h
	install -D -m 0755 tests/saas-test-authenticate          $(BINDIR)/saas-test-authenticate
	install -D -m 0755 tests/saas-test-gid-to-groupname      $(BINDIR)/saas-test-gid-to-groupname
	install -D -m 0755 tests/saas-test-groupname-to-gid      $(BINDIR)/saas-test-groupname-to-gid
	install -D -m 0755 tests/saas-test-group-info            $(BINDIR)/saas-test-group-info
	install -D -m 0755 tests/saas-test-uid-to-username       $(BINDIR)/saas-test-uid-to-username
	install -D -m 0755 tests/saas-test-username-to-uid       $(BINDIR)/saas-test-username-to-uid
	install -D -m 0755 tests/saas-test-user-info             $(BINDIR)/saas-test-user-info
	install -D -m 0644 LICENSE                               $(DOCDIR)/LICENSE
	install -D -m 0644 README                                $(DOCDIR)/README
	install -D -m 0644 doc/*                                 $(DOCDIR)





